package com.cgnpc.scp.common.utils;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MybatisUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(MybatisUtil.class);

    /**
     * SqlSession需要关闭吗？？？
     */
    public static void closeSqlSession(SqlSession sqlSession) {
        if (sqlSession != null) {
            try {
                if (sqlSession instanceof SqlSessionTemplate) {
                    // org.mybatis.spring.SqlSessionTemplate,mybatis3.1.1中，源码close()直接丢出异常，不需要关闭。
                    // SqlSessionTemplate sqlSessionTemplate = (SqlSessionTemplate) sqlSession;
                    // org.mybatis.spring.SqlSessionUtils.closeSqlSession(sqlSessionTemplate, sqlSessionTemplate.getSqlSessionFactory());
                } else {
                    sqlSession.close();
                }
            } catch (Throwable t) {
                LOGGER.error("MybatisUtil.closeSqlSession.error.", t);
            }
        }
    }

}
